package tankgame.tankgame2;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyTankGame2 extends JFrame {
	MyPanel mp;

	public static void main(String[] args) {
		MyTankGame2 myTankGame2 = new MyTankGame2();
	}

	public MyTankGame2() {
		mp = new MyPanel();
		this.add(mp);
		this.addKeyListener(mp);
		this.setSize(500, 400);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}

class MyPanel extends JPanel implements KeyListener {
	Hero hero;
	Vector<Enemy> enemies = new Vector<Enemy>();
	int enemSize = 6;

	public MyPanel() {
		hero = new Hero(100, 270, 0, 1, Color.YELLOW);
		for (int i = 0; i < enemSize; i++) {
			Enemy enemy = new Enemy((i + 1) * 50, 0, 1, 1, Color.BLUE);
			enemies.add(enemy);
		}
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.fillRect(0, 0, 400, 300);
		drawTank(g, hero.getX(), hero.getY(), hero.getDirect(), hero.getColor());
		for (int i = 0; i < enemies.size(); i++) {
			drawTank(g, enemies.get(i).getX(), enemies.get(i).getY(), enemies.get(i).getDirect(),
					enemies.get(i).getColor());
		}
	}

	public void drawTank(Graphics g, int x, int y, int direct, Color color) {
		g.setColor(color);
		switch (direct) {
		case 0:// 向上
			g.fill3DRect(x, y, 5, 30, false);
			g.fill3DRect(x + 15, y, 5, 30, false);
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			g.fillOval(x + 5, y + 10, 10, 10);
			g.drawLine(x + 10, y + 15, x + 10, y);
			break;
		case 1:// 向下
			g.fillRect(x, y, 5, 30);
			g.fillRect(x + 15, y, 5, 30);
			g.fill3DRect(x + 5, y + 5, 10, 20, false);
			g.fillOval(x + 5, y + 10, 10, 10);
			g.drawLine(x + 10, y + 15, x + 10, y + 30);
			break;
		case 2:// 向左
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			g.fillOval(x + 10, y + 5, 10, 10);
			g.drawLine(x + 15, y + 10, x, y + 10);
			break;
		case 3:// 向右
			g.fill3DRect(x, y, 30, 5, false);
			g.fill3DRect(x, y + 15, 30, 5, false);
			g.fill3DRect(x + 5, y + 5, 20, 10, false);
			g.fillOval(x + 10, y + 5, 10, 10);
			g.drawLine(x + 15, y + 10, x + 30, y + 10);
			break;
		}

	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyChar() == 'w') {
			hero.setDirect(0);
			hero.moveUp();
		} else if (e.getKeyChar() == 's') {
			hero.setDirect(1);
			hero.moveDown();
		} else if (e.getKeyChar() == 'a') {
			hero.setDirect(2);
			hero.moveLeft();
		} else if (e.getKeyChar() == 'd') {
			hero.setDirect(3);
			hero.moveRight();
		}
		repaint();
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

}

class Tank {
	int x = 0;// 横坐标
	int y = 0;// 纵坐标
	int direct;// 0表示上，1表示下，2表示左，3表示右
	int speed;// 速度
	Color color;// 颜色

	public Tank(int x, int y, int direct, int speed, Color color) {
		super();
		this.x = x;
		this.y = y;
		this.direct = direct;
		this.speed = speed;
		this.color = color;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getDirect() {
		return direct;
	}

	public void setDirect(int direct) {
		this.direct = direct;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public void moveUp() {
		y -= speed;
	}

	public void moveDown() {
		y += speed;
	}

	public void moveLeft() {
		x -= speed;
	}

	public void moveRight() {
		x += speed;
	}
}

class Hero extends Tank {
	public Hero(int x, int y, int direct, int speed, Color color) {
		super(x, y, direct, speed, color);
	}
}

class Enemy extends Tank {
	public Enemy(int x, int y, int direct, int speed, Color color) {
		super(x, y, direct, speed, color);
	}

}